package com.yangtzeu.jwc.utils;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AlertDialog;

public class NoticeDialog {
    int dialogResult;
    public static Handler mHandler;
    public Context context;

    public static boolean show(Activity context, String title, String msg) {
        mHandler = new MyHandler();
        return new NoticeDialog(context, title, msg).getResult() == 1;
    }

    public NoticeDialog(Activity context, String title, String msg) {
        //显示Dialog;
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setPositiveButton("知道了", new DialogButtonOnClick(1));
        dialogBuilder.setNegativeButton("不再提醒", new DialogButtonOnClick(0));
        dialogBuilder.setTitle(title).setMessage(msg);
        AlertDialog dialog = dialogBuilder.create();
        dialog.setCancelable(false);
        dialog.show();
        //系统线程会阻塞
        try {
            Looper.loop();
        } catch (Exception e) {
        }
    }

    public int getResult() {
        return dialogResult;
    }

    private static class MyHandler extends Handler {
        public void handleMessage(Message mesg) {
            throw new RuntimeException();
        }
    }

    private final class DialogButtonOnClick implements OnClickListener {
        int type;

        public DialogButtonOnClick(int type) {
            this.type = type;
        }

        /**
         * 用户点击确定或取消按钮后,设置点击按钮,发送消息;
         * mHandler收到消息后抛出RuntimeException()异常;
         * 阻塞会消失,主线程继续执行
         */
        public void onClick(DialogInterface dialog, int which) {
            NoticeDialog.this.dialogResult = type;
            Message m = mHandler.obtainMessage();
            mHandler.sendMessage(m);
        }
    }

}

